package young.dao;

import young.pojo.UserDO;

import java.sql.*;

public abstract class BaseDAO {

    protected Connection con;
    protected PreparedStatement statement;
    protected ResultSet rs;

    public Connection getConnection() {

        // 1. 加载驱动
        try{
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        // 2. 创建连接
         con = null;
        try {
            con = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/young?userSSL=true&useUnicode=true&characterEncoding=UTF8&serverTimezone=GMT",
                    "root", "5911591");
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        System.out.println("con:" + con);


        return con;


    }

    //调取数据
    public UserDO getByUsernameAndPwd(String username, String pwd) {
        UserDO userDO = null;
        //创建连接
         con = getConnection();
         statement = null;
         rs = null;
        String sql = "select * from t_atm where atm_id=? and atm_pwd=?";
        //查询调取
        try {
            statement = con.prepareStatement(sql);
            statement.setObject(1, username);
            statement.setObject(2, pwd);
            rs = statement.executeQuery();


            while (rs.next()) {
                userDO = new UserDO();
                userDO.setId(rs.getString(1));
                userDO.setPassword(rs.getString(2));
                userDO.setMoney(rs.getInt(3));


            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
                if (statement != null) {
                    try {
                        statement.close();
                    } catch (SQLException throwables) {
                        throwables.printStackTrace();
                    }
                    if (rs != null) {
                        try {
                            rs.close();
                        } catch (SQLException throwables) {
                            throwables.printStackTrace();
                        }
                    }
                }
            }

        }
        return userDO;


    }


    public static void guanbi(Connection con, Statement statement) {
        if (con != null) {
            try {
                con.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    public UserDO getById(String id) {
        con = getConnection();
        statement = null;
        rs = null;
        UserDO userDO = null;
        String sql = "select * from t_atm  where atm_id=?";
        try {
            statement = con.prepareStatement(sql);
            statement.setObject(1, id);
            rs = statement.executeQuery();

            if (rs.next()) {
                userDO = new UserDO();
                userDO.setId(rs.getString(1));
                userDO.setPassword(rs.getString(2));
                userDO.setMoney(rs.getInt(3));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            guanbi(con, statement, rs);
        }
        return userDO;
    }


    /**
     * 根据User对象中的数据进行修改
     *
     * @param
     */
    public void update(UserDO userDO1, UserDO userDO2) {
        // 1. 创建连接
         con = getConnection();
         statement = null;
        String sql = "update t_atm set atm_pwd=?,atm_money=? where atm_id=?";
        try {
            // 2. 获取执行SQL语句对象
            con.setAutoCommit(false);
            statement = con.prepareStatement(sql);
            statement.setObject(1, userDO1.getPassword());
            statement.setObject(2, userDO1.getMoney());
            statement.setObject(3, userDO1.getId());
            statement.executeUpdate();

            statement.setObject(1, userDO2.getPassword());
            statement.setObject(2, userDO2.getMoney());
            statement.setObject(3, userDO2.getId());
            statement.executeUpdate();
            con.commit();

        } catch (SQLException throwables) {
            throwables.printStackTrace();

            try {
                con.rollback(); //异常回滚
            } catch (SQLException e) {
                e.printStackTrace();
            }


        } finally {
            // 4. 关闭连接
            guanbi(con, statement);
        }
    }


    private void guanbi(Connection con, Statement statement, ResultSet rs) {

    }


}

